package com.yanqu.road.dao.impl.activity.shuihuyezicard;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardBattleRecordData;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/12/23 12:04
 */
public class ShuiHuYeZiCardBattleRecordDataDaoImpl extends TempDataObjectDao<ShuiHuYeZiCardBattleRecordData> {

    public static String getShuiHuYeZiCardBattleRecordDataSql = "select * from `t_u_activity_shuihuyezicard_battle_record_data` where `activityId`=? and `userId`=? ORDER BY `startTime` asc ";
    public List<ShuiHuYeZiCardBattleRecordData> getShuiHuYeZiCardBattleRecordDataList(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<ShuiHuYeZiCardBattleRecordData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getShuiHuYeZiCardBattleRecordDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateShuiHuYeZiCardBattleRecordDataSql = "update `t_u_activity_shuihuyezicard_battle_record_data` set roomType=?,remainCardNum=?,addRankScore=?,curRankScore=?,reward=?,isWin=?,targetRemainCardNum=?,targetInfo=?,npcId=?,startTime=?,endTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(ShuiHuYeZiCardBattleRecordData shuiHuYeZiCardBattleRecordData) {
        shuiHuYeZiCardBattleRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getRoomType()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getRemainCardNum()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getAddRankScore()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getCurRankScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, shuiHuYeZiCardBattleRecordData.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, shuiHuYeZiCardBattleRecordData.getIsWin()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getTargetRemainCardNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardBattleRecordData.getTargetInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getNpcId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardBattleRecordData.getStartTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardBattleRecordData.getUserId()));
        if (getDb().executeNoneQuery(updateShuiHuYeZiCardBattleRecordDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertShuiHuYeZiCardBattleRecordDataSql = "insert into `t_u_activity_shuihuyezicard_battle_record_data`(activityId,userId,roomType,remainCardNum,addRankScore,curRankScore,reward,isWin,targetRemainCardNum,targetInfo,npcId,startTime,endTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ShuiHuYeZiCardBattleRecordData shuiHuYeZiCardBattleRecordData) {
        shuiHuYeZiCardBattleRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardBattleRecordData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getRoomType()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getRemainCardNum()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getAddRankScore()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getCurRankScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, shuiHuYeZiCardBattleRecordData.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, shuiHuYeZiCardBattleRecordData.getIsWin()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getTargetRemainCardNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardBattleRecordData.getTargetInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getNpcId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardBattleRecordData.getStartTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardBattleRecordData.getEndTime()));
        if (getDb().executeNoneQuery(insertShuiHuYeZiCardBattleRecordDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ShuiHuYeZiCardBattleRecordData parseData(ResultSet rs) throws SQLException {
        ShuiHuYeZiCardBattleRecordData shuiHuYeZiCardBattleRecordData = new ShuiHuYeZiCardBattleRecordData();
        shuiHuYeZiCardBattleRecordData.setActivityId(rs.getInt("activityId"));
        shuiHuYeZiCardBattleRecordData.setUserId(rs.getLong("userId"));
        shuiHuYeZiCardBattleRecordData.setRoomType(rs.getInt("roomType"));
        shuiHuYeZiCardBattleRecordData.setRemainCardNum(rs.getInt("remainCardNum"));
        shuiHuYeZiCardBattleRecordData.setAddRankScore(rs.getInt("addRankScore"));
        shuiHuYeZiCardBattleRecordData.setCurRankScore(rs.getInt("curRankScore"));
        shuiHuYeZiCardBattleRecordData.setReward(rs.getString("reward"));
        shuiHuYeZiCardBattleRecordData.setIsWin(rs.getBoolean("isWin"));
        shuiHuYeZiCardBattleRecordData.setTargetRemainCardNum(rs.getInt("targetRemainCardNum"));
        shuiHuYeZiCardBattleRecordData.setTargetInfo(JSON.parseObject(rs.getString("targetInfo"), UserBaseInfo.class));
        shuiHuYeZiCardBattleRecordData.setStartTime(rs.getLong("startTime"));
        shuiHuYeZiCardBattleRecordData.setEndTime(rs.getInt("endTime"));
        shuiHuYeZiCardBattleRecordData.setNpcId(rs.getInt("npcId"));

        return shuiHuYeZiCardBattleRecordData;
    }
}
